package com.imay.live.manager;

import android.app.Activity;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.imay.live.helper.EventHelper;
import com.imay.live.model.UnreadModel;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.tbas_api.IMYTBASObject;

/**
 * Created by  nick on 2017/5/15 0015.
 * 未读红点管理
 */

public class UnreadManager {

    public static final int TYPE_UNREAD_SYSTEM_PROFILE =1;
    public static final int TYPE_UNREAD_MESSAGE =3;
    public static final int TYPE_UNREAD_FAMILY_MESSAGE =8;
    public static final int TYPE_UNREAD_CALL_LIVE_PROFILE =252;
    public static final int TYPE_UNREAD_SYSTEM = 5;
    private IMYTBASObject mSocketNotify;

    private static class UnreadManagerHolder {
        private static final UnreadManager INSTANCE = new UnreadManager();
    }

    public static final UnreadManager getInstance() {
        return UnreadManagerHolder.INSTANCE;
    }

    private UnreadManager() {
    }

    /**
     * 添加socket 监听
     */
    public void registerSocketListener(final ChangeUnreadStatusListener changeUnreadStatusListener) {
        //监听socket
        mSocketNotify = new IMYTBASObject(false);
        mSocketNotify.stringCommand = EventHelper.EVENT_UNREAD;
        mSocketNotify.setDataResultListener(new IMYTBASObject.OnDataResultListener() {
            @Override
            public void onCommandTaskSuccess(IMYTBASObject object) {
                Log.e("unread", "onCommandTaskSuccess: " + object.stringData);
                UnreadModel protoResult = JSON.parseObject(object.stringData, UnreadModel.class);
                changeUnreadStatusListener.onStatusChange(protoResult);
            }
        });
        mSocketNotify.addEventListenWithTBASObject();
    }

    public void unregisterSocketListener() {
        if (mSocketNotify != null) {
            mSocketNotify.destroy();
        }
    }

    public void postUnreadEvent(int type, int count) {
        UnreadModel model = new UnreadModel();
        model.setWarnType(type);
        model.setValue(count);
        EventHelper.postUnread(JSON.toJSONString(model));

    }

    public interface ChangeUnreadStatusListener {
        void onStatusChange(UnreadModel protoResult);
    }

    public void clearRecentUnread(long uid) {
        ProtoRequestHelper.task(ProtoHttpFactory.setRecentContactReaded(uid)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {

            }

            @Override
            public void onRequestError(String command, int code, String message) {

            }
        });
    }
}
